package com.sy.musicapp.ui.home

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Logout
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberImagePainter
import coil.transform.CircleCropTransformation
import com.sy.musicapp.R
import com.sy.musicapp.model.UserInfo
import com.sy.musicapp.ui.common.noRippleClickable
import com.sy.musicapp.ui.theme.Global
import com.sy.musicapp.ui.theme.MusicAppTheme
import com.sy.musicapp.ui.theme.tintText
import java.text.SimpleDateFormat
import java.util.*

/**
 * 用户信息卡片组件
 * @author SY
 * @since 2022-02-07 21:34
 **/
@Composable
fun UserProfileCard(userInfo: UserInfo, logout: () -> Unit) {
    Card(backgroundColor = Global.colors.primary, elevation = 0.dp) {
        Column(modifier = Modifier.padding(horizontal = 25.dp, vertical = 10.dp)) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.fillMaxWidth(1f)
            ) {
                Image(
                    painter = rememberImagePainter(
                        data = userInfo.avatarUrl,
                        builder = {
                            placeholder(R.drawable.keli)
                            error(R.drawable.keli)
                            crossfade(true)
                            transformations(CircleCropTransformation())
                        }
                    ),
                    contentDescription = "avatar",
                    modifier = Modifier.size(50.dp)
                )
                Column(
                    verticalArrangement = Arrangement.SpaceBetween,
                    modifier = Modifier
                        .padding(start = 10.dp)
                        .weight(0.6f)
                ) {
                    Text(text = userInfo.nickname, color = Global.colors.secondary)
                    Text(
                        text = SimpleDateFormat("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE).format(
                            userInfo.createTime
                        ),
                        style = tintText
                    )
                }
                Icon(
                    imageVector = Icons.Rounded.Logout,
                    contentDescription = "logout",
                    modifier = Modifier.noRippleClickable { logout() })
            }
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween,
                modifier = Modifier
                    .padding(top = 10.dp)
                    .fillMaxWidth(1f)
            ) {
                Text(buildAnnotatedString {
                    append("个人介绍:")
                    withStyle(style = SpanStyle(color = Global.colors.onSurface)) {
                        append(
                            userInfo.signature ?: "${UserInfo.Empty.signature}"
                        )
                    }
                }, style = tintText, letterSpacing = 1.5.sp)
            }
        }
    }
}

@Preview
@Composable
fun UserProfilePrev() {
    MusicAppTheme {
//        UserProfileCard(UserInfo.Empty)
    }
}

@Preview
@Composable
fun UserProfilePrevDark() {
    MusicAppTheme(darkTheme = true) {
//        UserProfileCard(UserInfo.Empty)
    }
}